﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Infrastructure.CrossCutting.Caching
{
    /// <summary>
    /// Represent a cache key information for Cache
    /// </summary>
    public sealed class CacheKey
    {
        #region Properties

        /// <summary>
        /// Get the key name for this cache key
        /// </summary>
        public string KeyName { get { return _keyName; } }
        private readonly string _keyName;

        /// <summary>
        /// Get the name value collection of elements that vary the cache item
        /// </summary>
        public Dictionary<string, string> CacheParameters { get { return _cacheParameters; } }
        private readonly Dictionary<string, string> _cacheParameters;

        #endregion

        #region Constructor

        public CacheKey(string keyName)
            : this(keyName, (Dictionary<string, string>)null) { }

        public CacheKey(string keyName, object parameters)
            : this(keyName, ExtractParameters(parameters)) { }

        public CacheKey(string keyName, Dictionary<string, string> parameters)
        {
            if (String.IsNullOrEmpty(keyName))
                throw new ArgumentNullException("keyName");

            _keyName = keyName;

            if (parameters != null && parameters.Count > 0)
                _cacheParameters = parameters;
            else
                _cacheParameters = new Dictionary<string, string>();
        }

        #endregion

        #region Private Methods

        private static Dictionary<string, string> ExtractParameters(object parameters)
        {
            Dictionary<string, string> cacheParameters = null;
            if (parameters != null)
            {
                Type anonymousType = parameters.GetType();
                var result = anonymousType
                    .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                    .ToDictionary(pi => pi.Name, pi => pi.GetValue(parameters, null).ToString());

                if (result.Count > 0)
                {
                    cacheParameters = result;
                }
            }
            return cacheParameters;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Get composed cache key
        /// </summary>
        /// <returns>String represent the key for cache item</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public string GetCacheKey()
        {
            var keyBuilder = new StringBuilder();
            keyBuilder.Append(_keyName);

            if (_cacheParameters != null && _cacheParameters.Count > 0)
            {
                keyBuilder.Append('#');

                foreach (var item in _cacheParameters)
                {
                    keyBuilder.Append(item.Key);
                    keyBuilder.Append(';');
                    keyBuilder.Append(item.Value);
                    keyBuilder.Append(';');
                }

                //remove last ;
                --keyBuilder.Length;
            }

            return keyBuilder.ToString();

        }

        public override string ToString()
        {
            return GetCacheKey();
        }

        #endregion
    }
}
